package com.econ.powercloud.ems.service.biz;

import com.econ.powercloud.ems.common.enums.ControlRecordStatusEnum;
import com.econ.powercloud.ems.common.enums.FaultDesc;
import com.econ.powercloud.ems.common.pojo.EmsMqttReplyVO;
import com.econ.powercloud.ems.dal.entity.EmsControlRecordDetail;
import com.econ.powercloud.ems.dal.entity.EmsGridRecordStep;
import com.econ.powercloud.ems.observer.ControlDealProcessor;
import com.econ.powercloud.ems.observer.ServiceFeedbackProcessor;
import com.econ.powercloud.ems.service.dao.EmsControlRecordDetailDAO;
import com.econ.powercloud.ems.service.dao.EmsGridRecordStepDAO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author : zjf
 * @Description :
 * @Date : 2022/8/19 8:57
 * @Modified by : zjf
 */
@Slf4j
@Service
public class EmsJobService {

    @Autowired
    private EmsGridRecordStepDAO emsGridRecordStepDAO;
    @Autowired
    private ServiceFeedbackProcessor serviceFeedbackProcessor;
    @Autowired
    private EmsControlRecordDetailDAO emsControlRecordDetailDAO;
    @Autowired
    private ControlDealProcessor controlDealProcessor;

    /**
     * 并离网下发操作未收到反馈值的查询
     * 表：ems_grid_node_record
     * 参数：status处于进行中/待确认,sync_msg_id不是null,并且当前时间大于创建时间5分钟的数据
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void dealWithGridTimeoutData(){
        List<EmsGridRecordStep> runningList = emsGridRecordStepDAO.findRunning();
        Date now = new Date();
        //超过2分钟没收到回复,则设置成超时
        List<EmsGridRecordStep> recordSteps = runningList.stream().filter(it->isMoreThanTwoMinute(it.getBeginTime(),now)).collect(Collectors.toList());
        recordSteps.stream().forEach(d->{
            serviceFeedbackProcessor.doInvoke(EmsMqttReplyVO.builder().msgId(d.getSyncMsgId()).code(FaultDesc.FAULT_TIMEOUT.getCode()).build());
        });
    }

    /**
     * 群控下发操作
     * 若超过两分钟还未收到反馈值，则置为失败
     */
    @Scheduled(cron = "0 0/2 * * * ?")
    public void controlTimeOut() {
        log.info("定时任务执行controlTimeOut");
        List<EmsControlRecordDetail> runningList = emsControlRecordDetailDAO.selectByStatusAndMsgIdIsNotNull(ControlRecordStatusEnum.IN_PROGRESS.getCode());
        Date now = new Date();
        List<EmsControlRecordDetail> list = runningList.stream().filter(it -> isMoreThanTwoMinute(new Date(it.getBeginTime()), now)).collect(Collectors.toList());
        list.stream().forEach(item -> {
            controlDealProcessor.doInvoke(EmsMqttReplyVO.builder().msgId(item.getMsgId()).code(FaultDesc.FAULT_TIMEOUT.getCode()).build());
        });
    }


    /**
     * 判断两个时间是否相差两分钟
     * firstTime: 时间(小)
     * secondTime: 时间(大)
     */
    private boolean isMoreThanTwoMinute(Date firstTime, Date secondTime) {
        return (secondTime.getTime() - firstTime.getTime()) > 1000 * 60 * 2;
    }
}
